package com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.peopleactivity.adapter.ImageGridAdapter;
import com.example.win.android_banduyujia.entity.ImageItem;
import com.example.win.android_banduyujia.peopleactivity.helper.AlbumHelper;
import com.example.win.android_banduyujia.peopleactivity.ui.bitmap.Bimp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Win on 2015/9/10.
 */
public class ImageGridActivity extends Activity{
    public static final String EXTRA_IMAGE_LIST = "imagelist";

    List<ImageItem>  dataList;
    GridView    gridView;
    ImageGridAdapter adapter;
    AlbumHelper   helper;
    Button    bt;

    Handler  mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    Toast.makeText(ImageGridActivity.this, "最多选择9张图片", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_image_grid_item);

        helper = AlbumHelper.getHelper();
        helper.init(getApplicationContext());

        dataList= (List<ImageItem>) getIntent().getSerializableExtra(EXTRA_IMAGE_LIST);

        initViews();
        bt = (Button)findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> list = new ArrayList<String>();
                Collection<String> c = adapter.map.values();
                Iterator<String> it = c.iterator();
                for (; it.hasNext(); ) {
                    list.add(it.next());
                }
                /*if (Bimp.act_bool) {
                    Intent intent = new Intent(ImageGridActivity.this, AssPictureActivity.class);
                    startActivity(intent);
                    Bimp.act_bool = false;
                }*/
                for (int i = 0; i < list.size(); i++) {
                    if (Bimp.drr.size() < 9) {
                        Bimp.drr.add(list.get(i));
                    }
                }
                finish();
            }
        });
    }

    private void initViews() {
        gridView=(GridView)findViewById(R.id.gridview);
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        adapter = new ImageGridAdapter(ImageGridActivity.this, dataList, mHandler);
        gridView.setAdapter(adapter);
        adapter.setTextCallback(new ImageGridAdapter.TextCallback() {

            @Override
            public void onListen(int count) {
                bt.setText("完成" + "(" + count + ")");
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.notifyDataSetChanged();
            }
        });
    }
}
